package cn.minhx.domain.activity.service.trial.task;

import cn.minhx.domain.activity.adapter.IActivityRepository;
import cn.minhx.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.minhx.domain.activity.model.valobj.SCSkuActivityVO;
import java.util.concurrent.Callable;

/**
 * 查询营销配置任务
 *
 * @author minhx
 */
public class QueryGroupBuyActivityDiscountVOTask implements Callable<GroupBuyActivityDiscountVO> {

	/** 活动ID */
	private final Long activityId;

	/** 来源 */
	private final String source;

	/** 渠道 */
	private final String channel;

	/** 商品ID */
	private final String goodsId;

	/** 活动仓储 */
	private final IActivityRepository activityRepository;

	public QueryGroupBuyActivityDiscountVOTask(Long activityId, String source, String channel, String goodsId,
			IActivityRepository activityRepository) {
		this.activityId = activityId;
		this.source = source;
		this.channel = channel;
		this.goodsId = goodsId;
		this.activityRepository = activityRepository;
	}

	@Override
	public GroupBuyActivityDiscountVO call() throws Exception {
		// 判断是否存在可用的活动ID
		Long availableActivityId = activityId;
		if (null == activityId) {
			// 查询渠道商品活动配置关联配置
			SCSkuActivityVO scSkuActivityVO = activityRepository.querySCSkuActivityBySCGoodsId(source, channel,
					goodsId);
			if (null == scSkuActivityVO) {
				return null;
			}
			availableActivityId = scSkuActivityVO.getActivityId();
		}

		return activityRepository.queryGroupBuyActivityDiscountVO(availableActivityId);
	}
}
